package com.banzhi.shop.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.data.MyShoperBean;
import com.banzhi.shop.model.CallBack.MyShoperCallBack;

import com.banzhi.shop.utils.DateUtil;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.bumptech.glide.Glide;
import com.github.library.BaseRecyclerAdapter;
import com.github.library.BaseViewHolder;
import com.github.library.listener.RequestLoadMoreListener;
import com.github.library.view.LoadType;
import com.nostra13.universalimageloader.utils.L;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.banzhi.shop.MyApp.context;

//我的店长
public class MyShoperActivity extends BaseActivity implements View.OnClickListener, MyShoperCallBack {

    private ImageView mIvBackShoper;
    private ImageView mIvSousuoShoper;
    private TextView mTvAllPeople;
    private SwipeRefreshLayout mSrf_shoper;
    private RecyclerView mRvShoper;
    private MyShoperImpl mMyShoperModel;
    private int page = 1;
    private String mShopownerCount;
    private MyShoperBean mMyShoperBean;

    List<MyShoperBean.ShopownerBean> Shopowners = new ArrayList<>();
    private LinearLayoutManager mLayoutManager;
    private BaseRecyclerAdapter<MyShoperBean.ShopownerBean> mBaseRecyclerAdapter;
    private String mSum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_shoper);
        Translucent.setTranslucent(this);
        initView();
        initListener();
        mMyShoperModel = new MyShoperImpl();
        LoadingDialogUtils.showProgress( this, "正在加载,请稍后~" );
        mMyShoperModel.getMyShoperData(page+"",this);

        Intent intent = getIntent();
        mSum = intent.getStringExtra("sum");

    }

    private void initView() {
        mIvBackShoper = (ImageView) findViewById(R.id.iv_back_shoper);
        mIvSousuoShoper = (ImageView) findViewById(R.id.iv_sousuo_shoper);
        mTvAllPeople = (TextView) findViewById(R.id.tv_all_people);
        mSrf_shoper = (SwipeRefreshLayout) findViewById(R.id.srf_shoper);
        mSrf_shoper.setColorSchemeResources(R.color.themeColor);
        mSrf_shoper.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        mRvShoper = (RecyclerView) findViewById(R.id.rv_shoper);
        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);

    }

    private void initListener() {
        mIvBackShoper.setOnClickListener(this);
        mIvSousuoShoper.setOnClickListener(this);
        mSrf_shoper.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mMyShoperModel.getMyShoperData(page+"",MyShoperActivity.this);
                mBaseRecyclerAdapter.notifyDataSetChanged();
                mSrf_shoper.setRefreshing(false);//刷新图标消失

            }
        });
/*        mRvShoper.addOnScrollListener(new EndlessRecyclerOnScrollListener( mLayoutManager) {
            @Override
            public void onLoadMore() {
                page++;
                LoadingDialogUtils.showProgress(MyShoperActivity.this, "正在加载,请稍后~");
                mMyShoperModel.getMyShoperData(page+"",MyShoperActivity.this);
            }
        });*/

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back_shoper:
                finish();
                break;
            case R.id.iv_sousuo_shoper:
                startActivity(new Intent(this,FindShoperActivity.class));
                break;
        }

    }

    @Override
    public void ongetMyShoperDataSuccess(Object object) {
        if (mSrf_shoper.isRefreshing()) {
            mSrf_shoper.setRefreshing(false);
        }
        mMyShoperBean = (MyShoperBean) object;
        List<MyShoperBean.ShopownerBean> shopowner = mMyShoperBean.getShopowner();
        if (page == 1) {
            Shopowners.clear();
        } else {
            if ( shopowner.size() == 0){
                return;
            }
        }

        Shopowners.addAll(shopowner);
        LogUtils.d("Shopowners.size():",Shopowners.size()+"");
        mShopownerCount = mMyShoperBean.getShopownerCount();
       // LogUtils.d("mShopownerCount",mShopownerCount);

         showView();
    }

    private void showView() {
        mTvAllPeople.setText(mSum );
        mBaseRecyclerAdapter = new BaseRecyclerAdapter<MyShoperBean.ShopownerBean>(this,Shopowners, R.layout.recyclerview_item_shopers) {
            @Override
            protected void convert(BaseViewHolder baseViewHolder, MyShoperBean.ShopownerBean bean) {
                baseViewHolder.setText(R.id.tv_nickname ,bean.getNickname());
                baseViewHolder.setText(R.id.tv_time ,bean.getAgenttime());
                baseViewHolder.setText(R.id.tv_mobile ,bean.getMobile());
                Glide.with(mContext).load( bean.getAvatar()).placeholder(R.mipmap.zhanweitu2).into((ImageView) baseViewHolder.getView(R.id.civ));
            }
        };
        mRvShoper.setLayoutManager( mLayoutManager);
        mRvShoper.setAdapter(mBaseRecyclerAdapter);

        mBaseRecyclerAdapter.openLoadAnimation(false); //false 不使用动画；

        mBaseRecyclerAdapter.openLoadingMore(true);
        mBaseRecyclerAdapter.setOnLoadMoreListener(new RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                LoadingDialogUtils.showProgress(MyShoperActivity.this, "正在加载,请稍后~");
                mMyShoperModel.getMyShoperData(page+"",MyShoperActivity.this);
                mBaseRecyclerAdapter.notifyDataSetChanged();
                mBaseRecyclerAdapter.addNoMoreView();
           /*      mBaseRecyclerAdapter.setNewData(list);
                 mBaseRecyclerAdapter.loadMoreComplete();*/

            }
        });

        //RecyclerView 调用notifyDataSetChanged()方法刷新后 自动滚动到顶部 问题解决办法
      //  mRvShoper.setFocusableInTouchMode(false);
       // mRvShoper.setFocusable(false);

        //  mBaseRecyclerAdapter.setLoadMoreType(LoadType.CUSTOM);
        //显示没有更多数据
        //  addFooterView();

    }

  /*  private void addFooterView() {
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View footerView = mInflater.inflate(R.layout.rv_foot_shoper, null);
        footerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        mBaseRecyclerAdapter.addFooterView(footerView);
        footerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
    }*/

    @Override
    public void ongetMyShoperDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));

    }
}
